package com.yycx.module.bus.provider.handler.haiying.custombalance;

import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.hy.BusPaymentOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 认款发货单收款单
 *
 * @author zyf
 */
@Component
public class HyYfkTab2PageListHandler implements PageInterceptor<BusPayment> {


    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusSalemanService busSalemanService;

    @Autowired
    private BusCustomService customService;

    @Autowired
    private FlowableHelp flowableHelp;

    @Autowired
    private BusPaymentOrderService paymentOrderService;

    @Autowired
    private BusBankStatementService bankStatementService;


    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String paymentNo = params.get("paymentNo");
        String amountType = params.get("amountType");
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = params.getLong("companyId", openUser.getCompanyId());
        cq.select(BusPayment.class, "*");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String customName = params.get("customName");
        if (FlymeUtils.isNotEmpty(amountType)) {
            cq.eq(true, "amountType", amountType);
        } else {
            //现款认款
            cq.in(true, "amountType",  AmountTypeEnum.OrderAmount.getCode());
        }
        cq.eq(true, "payment.companyId", companyId);
        if (FlymeUtils.isNotEmpty(customName)) {
            cq.like("payment.payUserName", customName);
        }
        cq.eq(true, "payment.advanceState", CommonConstants.INT_0);
        cq.like("payment.paymentNo", paymentNo);
        String userName = openUser.getUsername();
        if (!userName.contains("admin")) {
            cq.eq("payment.userId", openUser.getUserId());
        }
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "payDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "payDate", endDate);
        cq.orderByDesc("payment.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            EntityMap map = flowableHelp.getTaskInfo(processInstanceId);
            Long deptId = entityMap.getLong("deptId");
            if (FlymeUtils.isNotEmpty(deptId)) {
                BusDept dept = deptService.getById(deptId);
                if (FlymeUtils.isNotEmpty(dept)) {
                    map.put("deptName", dept.getDeptName());
                    map.put("deptType", dept.getDeptType());
                }
            }
            Long saleManId = entityMap.getLong("saleManId");
            if (FlymeUtils.isNotEmpty(saleManId)) {
                BusSaleman saleMan = busSalemanService.getById(saleManId);
                if (FlymeUtils.isNotEmpty(saleMan)) {
                    map.put("saleManName", saleMan.getSaleManName());
                }
            }
            Long customId = entityMap.getLong("customId");
            String payUserName = entityMap.get("payUserName");
            if (FlymeUtils.isEmpty(payUserName)) {
                BusCustom custom = customService.getById(customId);
                if (FlymeUtils.isNotEmpty(custom)) {
                    payUserName = custom.getCustomName();
                }
            }
            //流水信息
            String bankStatementId = entityMap.get("bankStatementId");
            if (FlymeUtils.isNotEmpty(bankStatementId)) {
                BusBankStatement bankStatement = bankStatementService.getById(bankStatementId);
                if (FlymeUtils.isEmpty(payUserName)) {
                    payUserName = bankStatement.getCustomName();
                }
                if (FlymeUtils.isNotEmpty(bankStatement)) {
                    entityMap.put("payBankNo", bankStatement.getBankNo());
                }
            }
            map.put("payUserName", payUserName);
            Long paymentId = entityMap.getLong("paymentId");
            BigDecimal amount = entityMap.getBigDecimal("amount");
            BigDecimal useAmount = paymentOrderService.totalByPaymentId(paymentId);
            BigDecimal surplusAmount = amount.subtract(useAmount);
            entityMap.put("surplusAmount", surplusAmount);
            entityMap.putAll(map);
        }
        return result;
    }

}
